package com.example.superoa.model.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.example.superoa.model.dao.ApplyDao;
import com.example.superoa.model.dao.ApprovalDao;
import com.example.superoa.model.dao.BecomeWorkerDao;
import com.example.superoa.model.dao.ContractDao;
import com.example.superoa.model.dao.DepartmentDao;
import com.example.superoa.model.dao.PositionDao;
import com.example.superoa.model.dao.RankApprovalDao;
import com.example.superoa.model.dao.RuzhiDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.HrDepartMapper;
import com.example.superoa.model.mappers.PositionMapper;
import com.example.superoa.model.pojos.Apply;
import com.example.superoa.model.pojos.Approval;
import com.example.superoa.model.pojos.BecomeWorker;
import com.example.superoa.model.pojos.Contract;
import com.example.superoa.model.pojos.Department;
import com.example.superoa.model.pojos.Position;
import com.example.superoa.model.pojos.RankApproval;
import com.example.superoa.model.pojos.Ruzhi;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.vo.HrDepartVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class HrDepartService {
	@Autowired
	private HrDepartMapper hm;
	@Autowired
	private StaffService staffservice;
	
	@Autowired
	private StaffDao sao;
	@Autowired
	private RuzhiDao rao;
	@Autowired
	private ApprovalDao apao;
	@Autowired
	private ApplyDao aao;
	@Autowired
	private DepartmentDao ddao;
	@Autowired
	private PositionDao pdao;
	@Autowired
	private ContractDao cao;
	@Autowired
	private PositionMapper pm;
	@Autowired
	private BecomeWorkerDao bao;
	@Autowired
	private RankApprovalDao raao;
	
	public List<HrDepartVo> initDepart(){
		List<HrDepartVo> list=hm.departmentAlls(0);
		for (HrDepartVo hrDepartVo : list) {
			List<HrDepartVo> list1=hm.departmentAlls(hrDepartVo.getId());
			for (HrDepartVo hrDepartVo1 : list1) {
				List<HrDepartVo> list2=hm.departmentAlls(hrDepartVo1.getId());
				for (HrDepartVo hrDepartVo2 : list2) {
					List<HrDepartVo> list3=hm.departmentAlls(hrDepartVo2.getId());
					for (HrDepartVo hrDepartVo3 : list3) {
						List<HrDepartVo> list4=hm.departmentAlls(hrDepartVo3.getId());
						
						hrDepartVo3.setChildren(list4);
					}
					
					hrDepartVo2.setChildren(list3);
				}
				
				hrDepartVo1.setChildren(list2);
			}
			
			hrDepartVo.setChildren(list1);
		}
		
		return list;
	}
	
	
	
	public int addDepart(HrDepartVo hr) {
		if(hr.getId()!=0) {//编辑
			hm.updateDepart(hr.getText(), hr.getDepartAddress(), hr.getId());
			return 2;
		}else {//新增
			hm.addDepart(hr.getText(), hr.getDepartAddress(), hr.getParentsid());
			return 1;
		}
	}
	
	
	public void updateDepart(int parentsid,int departPid) {
		hm.updateDeparttwo(parentsid, departPid);
	}
	
	public int deleteDepart(int departPid) {
		List<HrDepartVo> list=hm.departmentAlls(departPid);
		JSONArray json=hm.selectStaffByDepart(departPid);
		if(list.size()==0&&json.size()==0) {
			hm.deleteDepart(departPid);
			return 1;
		}else{
			return 2;
		}
	}
	
	
	//新增职位、部门与职级的关系记录
	public void addPositionDepart(int postion_id,int Depart_id,int rank_id) {
		hm.addPositionDepart(postion_id, Depart_id, rank_id);
	}
	//查询所有职位、部门与职级的记录
	public JSONArray selectPositionDepart() {
		return hm.selectPositionDepart();
	}
	
	public JSONArray selectrank() {
		return hm.selectrank();
	}
	public int deletepdr(int postion_id,int Depart_id,int rank_id) {
		JSONArray json=hm.selectStaffof(postion_id, Depart_id, rank_id);
		System.err.println("长度ha："+json.size());
		if(json.size()==0) {
		hm.deletepdr(postion_id, Depart_id, rank_id);
		return 2;
		}else {
		return 1;
	}
	}
	
	
	public JSONArray selectDairuzhi() {
		return hm.selectDairuzhi();
	}
	
	public void ruzhicommit(Staff staff) {
		//修改参数
		sao.save(staff);
	}
	public JSONArray selectDairuzhishpi(int id) {
		return hm.selectDairuzhishpi(id);
	}
	//发起审批入职，应该找到之前的入职id，补充或修改记录，然后需要新增一个申请请求
	public void ruzhi(int shqiren,int shpiren,int id,Date birthday, Date dateentry, String education, String idaddress, String localaddress,
			String marriage, String national, String qq, String name, String phone, String political, String sex,
			String tupian, int staffType, String urgent, String urgentPhone, String weixin,
			int zhengjian, String zhengjianhao, Date ht_start, Date ht_end, Date ht_finish,int Depart_id,
			int position_id, int ht_type) {
		
		Ruzhi rz=rao.findById(id).get();
		rz.setBirthday(birthday);
		rz.setDateentry(dateentry);
		rz.setEducation(education);
		rz.setIdaddress(idaddress);
		rz.setLocaladdress(localaddress);
		rz.setMarriage(marriage);
		rz.setNational(national);
		rz.setQq(qq);
		rz.setName(name);
		rz.setPhone(phone);
		rz.setPolitical(political);
		rz.setSex(sex);
		rz.setTupian(tupian);
		rz.setStaffType(staffType);
		rz.setUrgent(urgent);
		rz.setUrgentPhone(urgentPhone);
		rz.setWeixin(weixin);
		rz.setZhengjian(zhengjian);
		rz.setZhengjianhao(zhengjianhao);
		rz.setHt_start(ht_start);
		rz.setHt_end(ht_end);
		rz.setHt_finish(ht_finish);
		rz.setDepart_id(Depart_id);
		rz.setPosition_id(position_id);
		rz.setHt_type(ht_type);
		System.err.println("打印合同类型id："+ht_type);
		
		Staff s1=sao.findById(shqiren).get();
		Staff s2=sao.findById(shpiren).get();
		//新增申请记录
		Apply a= new Apply();
		a.setApplyType("入职申请");
		a.setStaff(s1);
		aao.save(a);
		//根据刚刚新增返回的申请id查询申请记录
		Apply a2=aao.findById(a.getApplyId()).get();
		
		Approval ap=new Approval();
		ap.setStaff(s2);
		ap.setPxapprovalState("未审批");
		ap.setApply(a2);
		apao.save(ap);
		
		
		rz.setShpi(a.getApplyId());
		//将发起入职审批的信息添加到入职表中
		rao.save(rz);  
	}
	
	
	public JSONArray selectAllStaffs() {
		return hm.selectAllStaffs();
	}
	public JSONArray selectStaffByDepartname(int Depart_id) {
		//根据部门id查询该部门的部门经理信息
		return hm.selectStaffByDepartname(Depart_id);
	}
	public void updateRuzhiById(int id) {
		hm.updateRuzhiById(id);
	}
	public JSONArray selectShpiBystaffid(int staff_id) {
		return hm.selectShpiBystaffid(staff_id,"未审批");
	}
	//根据入职id进入审批界面查询
	public JSONArray selectShpiByid(int id) {
		return hm.selectShpiByid(id);
	}
	
	public JSONArray selectRuzhiBysfzz() {
		//查询放弃入职员工，0代表未入职，1代表放弃入职，2代表已入职
		return hm.selectRuzhiBysfzz(1);
	}
	public JSONArray selectShpiAll() {
		//查询入职审核结果
		return hm.selectShpiAll();
	}
	//入职审批通过
	public void sureruzhi(int id) {
		Ruzhi rz=rao.findById(id).get();//找到这条id的记录
		rz.setSfzz(2);//0代表待入职，1代表放弃入职，2代表已入职
		rao.save(rz);
		
		Apply a=aao.findById(rz.getShpi()).get();//找到这个入职信息的申请信息
		
		int hmb=hm.selectApprovalByApplyid(a.getApplyId());//审批id
		Approval ap=apao.findById(hmb).get();
		ap.setPxapprovalState("已审核");
		ap.setPxapprovalIf("已通过");
		apao.save(ap);
		 
		Staff staff=new Staff();//新增员工
		staff.setStaff_name(rz.getName());
		staff.setBirthday(rz.getBirthday());
		staff.setDateentry(rz.getDateentry());
		staff.setEducation(rz.getEducation());
		staff.setIdaddress(rz.getIdaddress());
		staff.setLocaladdress(rz.getLocaladdress());
		staff.setMarriage(rz.getMarriage());
		staff.setNational(rz.getNational());
		staff.setQq(rz.getQq());
		staff.setStaff_phone(rz.getPhone());
		staff.setStaff_Political(rz.getPolitical());
		staff.setStaff_sex(rz.getSex());
		staff.setStaff_tupian(rz.getTupian());
		staff.setStaffType(rz.getStaffType());
		staff.setUrgent(rz.getUrgent());
		staff.setUrgentPhone(rz.getUrgentPhone());
		staff.setWeixin(rz.getWeixin());
		staff.setZhengjian(rz.getZhengjian());
		staff.setZhengjiannumber(rz.getZhengjianhao());
		staff.setStaff_sfzz("是");//是否在职
		staff.setStaff_state(0);//员工状态，0代表未转正，1代表已发起转正，默认为0
		staff.setStaff_pwd("000000");//原始密码
		
		Contract c=new Contract();//新建合同
		c.setContract_TypeName(rz.getHt_type());
		c.setStartDate(rz.getHt_start());
		c.setEndDate(rz.getHt_end());
		c.setTryEndDate(rz.getHt_finish());
		cao.save(c);
		
		
		Department depart=ddao.findById(rz.getDepart_id()).get();//找到部门id的记录
		
		staff.setDepartment(depart);
		staff.setContract(c);
		sao.save(staff);
	
		pm.saveRoleStaff(staff.getStaffId(), rz.getPosition_id());//给这个用户添加职位
		
		BecomeWorker bw=new BecomeWorker();//新增一个转正表记录
		bw.setStaff(staff);
		bw.setBwr_Date(rz.getHt_finish());
		bw.setBwr_state(0);//转正状态：0代表未转正，1代表已转正
		bao.save(bw);
		
	}
	
	//入职驳回
	public void falseruzhi(int id,String approval_reason) {
		Ruzhi rz=rao.findById(id).get();//找到这条id的记录
		rz.setSfzz(1);//0代表待入职，1代表放弃入职，2代表已入职
		rao.save(rz);
		
		Apply a=aao.findById(rz.getShpi()).get();//找到这个入职信息的申请信息
		
		int hmb=hm.selectApprovalByApplyid(a.getApplyId());//审批id
		Approval ap=apao.findById(hmb).get();
		ap.setPxapprovalState("已审核");
		ap.setPxapprovalIf("未通过");
		ap.setApproval_reason(approval_reason);
		apao.save(ap);
	}
	
	public JSONArray selectPositive() {
		return hm.selectPositive();
	}
	public JSONArray selectPositiveBystaffid(int staff_id) {
		Staff s=sao.findById(staff_id).get();
		s.setStaff_state(1);//0代表未转正，1代表已发起转正申请
		sao.save(s);
		
		return hm.selectPositiveBystaffid(staff_id);
	}
	
	public void positiveapproval(Date BecomeDate,Date salarydate,int shqiren,int shpiren,
			int Bwr_id,String reasondd) {
		BecomeWorker bw=bao.findById(Bwr_id).get();//找到该转正表的id
		bw.setBecomeDate(BecomeDate);//转正日期
		bw.setBw_reason(reasondd);//转正原因
		bw.setSalarydate(salarydate);//薪资生效时间
		

		Staff s1=sao.findById(shqiren).get();
		Staff s2=sao.findById(shpiren).get();
		//新增申请记录
		Apply a= new Apply();
		a.setApplyType("转正申请");
		a.setStaff(s1);
		aao.save(a);
		//根据刚刚新增返回的申请id查询申请记录
		Apply a2=aao.findById(a.getApplyId()).get();
		
		Approval ap=new Approval();
		ap.setStaff(s2);
		ap.setPxapprovalState("未审批");
		ap.setApply(a2);
		apao.save(ap);
		
		bw.setShqi(a.getApplyId());
		
		bao.save(bw);
	}
	//转正通过
	public void surepositive(int Bwr_id) {
		BecomeWorker bw=bao.findById(Bwr_id).get();
		bw.setBwr_state(1);
		bao.save(bw);
		
		Apply a=aao.findById(bw.getShqi()).get();
		
		int hmb=hm.selectApprovalByApplyid(a.getApplyId());//审批id
		Approval ap=apao.findById(hmb).get();
		ap.setPxapprovalState("已审核");
		ap.setPxapprovalIf("已通过");
		apao.save(ap);
	}
	//转正驳回
		public void falsezhuanzheng(int Bwr_id,String approval_reason) {
			BecomeWorker bw=bao.findById(Bwr_id).get();//找到这条id的记录
			bw.setBwr_state(0);
			int aa=bw.getStaff().getStaffId();
			bao.save(bw);
			//驳回后改回转正状态
			Staff s=sao.findById(aa).get();
			s.setStaff_state(0);
			sao.save(s);
			
			Apply a=aao.findById(bw.getShqi()).get();//找到这个转正信息的申请信息
			
			int hmb=hm.selectApprovalByApplyid(a.getApplyId());//审批id
			Approval ap=apao.findById(hmb).get();
			ap.setPxapprovalState("已审核");
			ap.setPxapprovalIf("未通过");
			ap.setApproval_reason(approval_reason);
			apao.save(ap);
		}
	//待转正
	public JSONArray daizhuanzheng(int staff_id) {
		return hm.daizhuanzheng(staff_id,"未审批");
	}
	//
	public JSONArray beforepositiveshpi(int staff_id) {
		return hm.beforepositiveshpi(staff_id);
	}
	//转正审批的结果
	public JSONArray positivestate() {
		return hm.positivestate();
	}
	//已转正
		public JSONArray positivestated() {
			return hm.positivestated("已通过");
		}
		
	//离职篇章走起：
	//修改员工在职状态	收回权限
	public void updateLizhi(int staff_id) {
		//离职修改
		hm.updateLizhi("否", staff_id);
		
	}
	//发起离职申请
	public void lizhigameover(Date shqiriqi,String lizhireason,String lizhiquxiang,
			Date salarydate,Date endworkdate,int shpiren,int shqiren) {
		RankApproval ra=new RankApproval();
		ra.setEndworkdate(endworkdate);
		ra.setRank_date(shqiriqi);
		ra.setRank_quxiang(lizhiquxiang);
		ra.setRank_Reason(lizhireason);
		ra.setRank_shpiren(shpiren);
		ra.setSalarydate(salarydate);
		
		Staff s=sao.findById(shqiren).get();
		
		ra.setStaff(s);
		raao.save(ra);
	}
	//查询所有离职审批----只有审批人才能看到
	public JSONArray selectdepartureByshpi(int shpiren) {
		return hm.selectdepartureByshpi(shpiren);
	}
	//通过离职审批（审批人）
	public void passdeparture(int rankid,Date rank_shpidate,String rank_bohui,int pd) {
		//pd为1代表确认离职
		if(pd==1) {
			RankApproval ra=raao.findById(rankid).get();
			ra.setRank_shpidate(rank_shpidate);
			ra.setRank_shpiresult("已通过");
			raao.save(ra);
		}else if(pd==2) {
			//pd为2代表驳回
			RankApproval ra=raao.findById(rankid).get();
			ra.setRank_shpidate(rank_shpidate);
			ra.setRank_shpiresult("未通过");
			ra.setRank_bohui(rank_bohui);
			raao.save(ra);
		}
		
	}
	public JSONArray selectdepartureByid(int rank_id) {
		return hm.selectdepartureByid(rank_id);
	}
	public JSONArray selectdepartureResult(int shpiren) {
		return hm.selectdepartureResult(shpiren);
	}
	public JSONArray selecthrstaff(int staff_id) {
		return hm.selectStaffxqByid(staff_id);
	}
}
